<template>
  <el-dialog :title=title
             v-if="open"
             :visible.sync="open"
             :modal=true
             width="80%"
             center
  >
    <div class="app-container">
      <div id="printInfo">

        <el-tabs type="border-card" v-model="activeTab">
          <el-tab-pane :key="1" label="采购计划信息" name="采购计划信息">
            <el-form ref="form" :model="form" :rules="rules" label-width="150px">
              <el-row>
                <el-col :span="10">
                  <el-form-item label="计划编号" prop="planNumber">
                    <el-input v-model="form.planNumber" readonly/>
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="计划名称" prop="planName">
                    <el-input v-model="form.planName" readonly/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="10">
                  <el-form-item label="开始日期" prop="planDate">
                    <el-date-picker clearable
                                    v-model="form.planDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择计划开始日期" style="width: 100%" readonly>
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="截止日期" prop="materialDeadline">
                    <el-date-picker clearable
                                    v-model="form.materialDeadline"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择计划截止日期" style="width: 100%" readonly>
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <!--      <el-row>
                      <el-col :span="10">
                        <el-form-item label="供应商" prop="vendorName">
                          <el-input v-model="form.vendorName" readonly/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="10">
                        <el-form-item label="付款方式" prop="paymentType">
                          <el-select v-model="form.paymentType" placeholder="请选择付款方式" style="width: 100%;" disabled>
                            <el-option
                              v-for="dict in dict.type.erp_payment_type"
                              :key="dict.value"
                              :label="dict.label"
                              :value="parseInt(dict.value)"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="10">
                        <el-form-item label="联系人" prop="contact">
                          <el-input v-model="form.contact" readonly/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="10">
                        <el-form-item label="联系人电话" prop="contactTel">
                          <el-input v-model="form.contactTel" readonly/>
                        </el-form-item>
                      </el-col>
                    </el-row>-->
              <el-row>
                <el-col :span="10">
                  <el-form-item label="申请人" prop="applyPersonName">
                    <el-input v-model="form.applyPersonName" readonly>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="申请部门" prop="applyDepartment">
                    <el-input v-model="form.applyDepartment" readonly>
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="20">
                  <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" type="textarea" placeholder="请输入备注" style="width: 100%"
                              maxlength="250" readonly/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <!--横线组件纯纯为了页面好看-->
            <el-divider></el-divider>
            <el-row style="margin-bottom: 20px;">
              <el-col :span="1.5">
                <div style="font-size: 20px; margin-top: 5px">采购物料</div>
              </el-col>
            </el-row>
            <el-table border :data="addGoodsList">
              <el-table-column label="物料编号" align="center" prop="itemCode"/>
              <el-table-column label="物料名称" align="center" prop="itemName"/>
              <el-table-column label="规格型号" align="center" prop="specification"/>
              <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
              <el-table-column label="分类" align="center" prop="itemTypeName"/>
              <el-table-column
                v-for="col in columnsConfig"
                :key="col.prop"
                :label="col.label"
                :prop="col.prop"
                :width="col.width"
                :align="col.align"
                v-if="col.show">
              </el-table-column>
              <el-table-column label="客户" align="center" prop="clientName" width="200" />
              <el-table-column label="备注" align="center" prop="remark" />
              <el-table-column label="采购数量" align="center" prop="quantity" />
              <el-table-column label="单价" align="center" prop="estimatedUnitPrice" />
              <el-table-column label="已采购数量" align="center" prop="purchasedQuantity" />
              <el-table-column label="已到货数量" align="center" prop="arrivedQuantity" />
              <el-table-column label="已入库数量" align="center" prop="rkNum" />
            </el-table>
          </el-tab-pane>
        </el-tabs>

        <div slot="footer" class="dialog-footer" style="text-align:center;margin-top: 20px;">
          <el-button
            type="danger"
            @click="submitForm"
            v-show="form.approvalStatus===0 || form.approvalStatus===4"
            v-hasPermi="['cg:plan:check']"
          >采购计划审批
          </el-button>
          <el-button
            :key="printButtonKey"
            type="primary"
            v-print="printInfoObj"
            ref="printButton"
            v-show="form.approvalStatus !== 0 && form.approvalStatus !== 4"
          >打印
          </el-button>
          <el-button type="info" @click="cancel">返 回</el-button>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {confirmPlan, getPlan} from "@/api/mes/cg/plan";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";
import UserSelect from "@/components/userSelect/single.vue";


export default {
  name: "infoView",
  components: {UserSelect},
  dicts: ['erp_payment_type'],
  data() {
    return {
      activeTab: '采购计划信息',
      open: false,
      title: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 添加的商品信息表格数据
      addGoodsList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      printInfoObj: {
        id: 'printInfo',
        popTitle: '采购计划',
        // preview: true,
        extraCss: '',
        extraHead: '<meta http-equiv="Content-Language" content="zh-cn"/>',
        beforeOpenCallback() {
        },
      },
      saveButtonKey: 0,
      printButtonKey: 0,
      columnsConfig:[],
    };
  },
  created() {
    /* this.getList();
     //查询商品表方法
     this.getGoodsList();
     //查询供应商列表方法
     this.getSupplierlist();*/
    this.getItemBase();
  },
  methods: {
    init(id) {
      getPlan(id).then(response => {
        this.form = response.data;
        this.addGoodsList = response.data.cgPurchasePlanDetails;
      });
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    //商品小计
    /* getSummaries(param) {
       const {columns, data} = param;
       const sums = []; //声明变量
       columns.forEach((column, index) => {
         if (index === 0) {
           sums[index] = "小计";
           return;
         }
         const values = data.map((item) => Number(item[column.property]));
         if (
           column.property === "total"
         ) {
           sums[index] = values.reduce((prev, curr) => {
             const value = Number(curr);
             if (!isNaN(value)) {
               return prev + curr;
             } else {
               return prev;
             }
           }, 0);
         } else {
           sums[index] = '';
         }
       });
       this.form.estimatedCost=sums[7];
       return sums;
     },*/
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 新增提交按钮 */
    submitForm() {
      let that = this;
      this.$modal.confirm('是否提交采购计划审批？').then(function () {
        confirmPlan(that.form.planId).then(response => {
          that.$modal.msgSuccess("采购计划已提交审批");
          that.open = false;
          that.$parent.getList();
          that.cancel();
        })
      });
    },
  }
};
</script>

<style scoped>
@media print {
  @page {
    size: auto;
    margin: 3mm;
  }

  body {
    height: auto;
    width: auto;
  }

  body * {
    visibility: hidden;
  }

  #printInfo, #printInfo * {
    visibility: visible;
  }

  #printInfo {
    position: relative;
    width: 100%;
    height: auto;
    margin: 0;
    padding: 0;
  }

  #printInfo .el-table {
    margin-top: 10px;
  }

  #printInfo .el-table table {
    width: 100% !important;
  }

  #printInfo .el-table .el-table__body,
  #printInfo .el-table .el-table__header {
    width: 100% !important;
  }

}
</style>
